<?php
namespace Admin\Controller;

class GoodController extends AdminController
{
    public function index()
    {
        $name          = I('get.name');
        $map['status'] = array('egt', 0);
        if (is_numeric($name)) {
            $map['id|name'] = array(intval($name), array('like', '%' . $name . '%'), '_multi' => true);
        } else {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }

        $list = $this->lists('good', $map);
        $cate = M('category')->getField('id,title');
        // dump($cate);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('cate', $cate);
        $this->meta_title = '商品';
        $this->display();

    }

    public function add()
    {

        $res = M('category')->where(['pid' => 0])->select();
        $this->assign('res', $res);

        if (IS_GET) {
            $this->display();
        } else {

            $_POST['pic'] = $this->upload('pic');
            $good         = D('Good');
            $data         = $good->create();
            if ($data) {
                $good->add($data);
                $this->success('添加商品成功', U('index'));
            } else {
                $this->error($good->getError());
            }
        }
    }

    public function del()
    {
        $this->display();
    }

    public function update()
    {
        $res1 = M('category')->where(['pid' => 0])->select();
        $this->assign('res1', $res1);
        $good = D('Good');

        if (IS_GET) {
            $res = $good->find(I('get.id'));
            $this->assign('res', $res);
            $this->display();
        } else {
            if ($_FILES['pic']['error'] != 4) {
                $_POST['pic'] = $this->upload('pic');
            }

            $data = $good->create();
            if ($data) {
                $good->where(['id' => I('post.id')])->save($data);
                $this->success('编辑成功', U('index'));
            } else {
                $this->error($good->getError());
            }
        }

    }

    public function attrvar()
    {   

        $a=I('get.');
        $this->display();
    }

    public function attrkey()
    {

        $this->display(); 
       
    }

    protected function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
// 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];

        }
    }

    public function changeStatus($method = null)
    {
        $id = array_unique((array) I('id', 0));

        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'hide':
                $this->forbid('Good', $map);
                break;
            case 'show':
                $this->resume('Good', $map);
                break;
            case 'del':
                D('Good')->where($map)->delete();
                $this->success('删除成功');
                break;
            default:
                $this->error('参数非法');
        }
    }

    public function ajax($fid = 0)
    {
        $fid = I('get.fid');
        $res = M('category')->where(['pid' => $fid])->select();
        if ($res) {
            $data['status'] = 1;
            $data['info']   = '成功';
            $this->assign('res', $res);
            $data['res'] = $this->fetch('ajax');
        } else {
            $data['status'] = 0;
            $data['info']   = '没有下一级内容';
            $data['res']    = null;
        }
        $this->ajaxReturn($data);
    }
}
